package Presentacion.contabilidad;

import global.Dialogos;
import global.Iconos;

/**
 *
 * @author Jose Daniel
 */
public final class dlgConcepto extends javax.swing.JDialog {
    private Iconos ico = new Iconos();
    public static frmConsultaConcepto concepto;

    /**
     * Creates new form dlgconcepto
     *
     * @param parent
     * @param modal
     */
    public dlgConcepto(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setResizable(false);
        setLocationRelativeTo(this);
        iconos();
        setTitle("REGISTRO DE CONCEPTO");
        frmConsultaConcepto.objConcepto.listarGrupo(cbogrupo);
        visiblidad(true);
        limpiar();
       
    }

    private void iconos() {
        btnnuevo.setIcon(ico.getNuevo());
        btnguardar.setIcon(ico.getGuardar());
        btnsalir.setIcon(ico.getSalir());
        btnactualizar.setIcon(ico.getActualizar());
    }

    public void limpiar() {
        txtconcepto.setText("");
        cbotipo.setSelectedIndex(0);
        txtprecio.setText("0.0");
        dlgConcepto.cbogrupo.setSelectedIndex(0);
        txtcuenta.setText("");
        dlgConcepto.cbogrupo.requestFocus();
    }

    public void llenardatos() {
        dlgConcepto.cbogrupo.setSelectedItem(frmConsultaConcepto.objConcepto.getNombre());
        txtcuenta.setText(frmConsultaConcepto.objConcepto.objCuenta.getDescripcion());
        txtconcepto.setText(frmConsultaConcepto.objConcepto.getConcepto());
        txtprecio.setText(Double.toString(frmConsultaConcepto.objConcepto.getPrecio()));
        cbotipo.setSelectedItem(frmConsultaConcepto.objConcepto.getTipo());
    }

    public void visiblidad(boolean visibilidad) {
        if (visibilidad != true) {
            btnactualizar.setVisible(true);
            btnguardar.setVisible(false);
        } else {
            btnactualizar.setVisible(false);
            btnguardar.setVisible(true);
        }
    }

    private boolean ValidacionCampos() {
        if ("".equals(txtconcepto.getText())) {
            Dialogos.FaltaCampo("Concepto");
            txtconcepto.requestFocus();
            return false;
        } else if ("--Seleccione--".equals(cbogrupo.getSelectedItem().toString())) {
            Dialogos.FaltaCampo("Grupo");
            cbogrupo.requestFocus();
            return false;
        } else if ("".equals(txtcuenta.getText())) {
            Dialogos.FaltaCampo("Cuenta");
            txtcuenta.requestFocus();
            return false;
        } else if ("0.0".equals(txtprecio.getText())) {
            Dialogos.FaltaCampo("Precio");
            txtprecio.requestFocus();
            return false;
        } else if ("--Seleccione--".equals(cbotipo.getSelectedItem().toString())) {
            Dialogos.FaltaCampo("tipo");
            cbotipo.requestFocus();
            return false;
        }
        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btngrupo = new javax.swing.JButton();
        btnCuenta = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtcuenta = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtconcepto = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cbogrupo = new javax.swing.JComboBox();
        txtprecio = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cbotipo = new javax.swing.JComboBox();
        jToolBar4 = new javax.swing.JToolBar();
        btnnuevo = new javax.swing.JButton();
        btnguardar = new javax.swing.JButton();
        btnactualizar = new javax.swing.JButton();
        btnsalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btngrupo.setText("+");
        btngrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btngrupoActionPerformed(evt);
            }
        });

        btnCuenta.setText("+");
        btnCuenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCuentaActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("CONCEPTO");

        txtcuenta.setEditable(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("CUENTA");

        txtconcepto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtconceptoKeyReleased(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("GRUPO");

        cbogrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbogrupoActionPerformed(evt);
            }
        });

        txtprecio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtprecioKeyTyped(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("PRECIO");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("TIPO");

        cbotipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "COBRO", "PAGO" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtconcepto)
                            .addComponent(cbogrupo, 0, 263, Short.MAX_VALUE))
                        .addGap(7, 7, 7)
                        .addComponent(btngrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cbotipo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtprecio, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(txtcuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(7, 7, 7)
                                .addComponent(btnCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtconcepto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbogrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btngrupo)
                    .addComponent(jLabel1))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCuenta)
                    .addComponent(jLabel2)
                    .addComponent(txtcuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtprecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(cbotipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
        );

        jToolBar4.setBorder(null);
        jToolBar4.setFloatable(false);
        jToolBar4.setBorderPainted(false);

        btnnuevo.setText("Nuevo");
        btnnuevo.setFocusable(false);
        btnnuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnnuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnnuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnuevoActionPerformed(evt);
            }
        });
        jToolBar4.add(btnnuevo);

        btnguardar.setText("Guardar");
        btnguardar.setFocusable(false);
        btnguardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnguardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnguardar);

        btnactualizar.setText("Actualizar");
        btnactualizar.setFocusable(false);
        btnactualizar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnactualizar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnactualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnactualizarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnactualizar);

        btnsalir.setText("Salir");
        btnsalir.setFocusable(false);
        btnsalir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnsalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsalirActionPerformed(evt);
            }
        });
        jToolBar4.add(btnsalir);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToolBar4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar4, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbogrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbogrupoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbogrupoActionPerformed

    private void btngrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btngrupoActionPerformed
        dlggrupo grupo = new dlggrupo(null, true);
        grupo.setVisible(true);
    }//GEN-LAST:event_btngrupoActionPerformed

    private void btnnuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnuevoActionPerformed
        limpiar();
        visiblidad(true);

    }//GEN-LAST:event_btnnuevoActionPerformed

    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        if (ValidacionCampos() == true) {
            frmConsultaConcepto.objConcepto.getIdGrupo(dlgConcepto.cbogrupo.getSelectedItem().toString());
            frmConsultaConcepto.objConcepto.setTipo(cbotipo.getSelectedItem().toString());
            frmConsultaConcepto.objConcepto.setConcepto(txtconcepto.getText());
            frmConsultaConcepto.objConcepto.setPrecio(Double.parseDouble(txtprecio.getText()));
            frmConsultaConcepto.objConcepto.objCuenta.sacarCodigo(txtcuenta.getText());
            if (frmConsultaConcepto.objConcepto.guardarConcepto() == true) {
                frmConsultaConcepto.generartabla();
                dispose();
                limpiar();
            } 
        }
    }//GEN-LAST:event_btnguardarActionPerformed

    private void btnactualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnactualizarActionPerformed
        if (ValidacionCampos() == true) {
            if (frmConsultaConcepto.objConcepto.ExistenciaConcepto(txtconcepto.getText()) == false) {
            frmConsultaConcepto.objConcepto.getIdGrupo(dlgConcepto.cbogrupo.getSelectedItem().toString());
            frmConsultaConcepto.objConcepto.setTipo(cbotipo.getSelectedItem().toString());
            frmConsultaConcepto.objConcepto.setConcepto(txtconcepto.getText());
            frmConsultaConcepto.objConcepto.setPrecio(Double.parseDouble(txtprecio.getText()));
            frmConsultaConcepto.objConcepto.objCuenta.sacarCodigo(txtcuenta.getText());
            
                if (frmConsultaConcepto.objConcepto.actualizarConcepto() == true) {
                    frmConsultaConcepto.generartabla();
                    dispose();
                    limpiar();
                }
            } else {
                Dialogos.Error("El concepto ya existe...");
            }
        }
    }//GEN-LAST:event_btnactualizarActionPerformed

    private void btnsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsalirActionPerformed
        dispose();
        limpiar();

    }//GEN-LAST:event_btnsalirActionPerformed

    private void txtprecioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtprecioKeyTyped
        global.Validaciones.soloNumeros(evt, txtprecio, 5);
    }//GEN-LAST:event_txtprecioKeyTyped

    private void btnCuentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCuentaActionPerformed
        dlgConsultaCuenta cuenta = new dlgConsultaCuenta(this, true);
        cuenta.setVisible(true);
        txtcuenta.setText(cuenta.descripcion);
        //concepto.objConcepto.objCuenta.setCodigo(cuenta.id);

    }//GEN-LAST:event_btnCuentaActionPerformed

    private void txtconceptoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtconceptoKeyReleased
        global.Validaciones.convertirMayuscula(txtconcepto);
    }//GEN-LAST:event_txtconceptoKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlgConcepto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlgConcepto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlgConcepto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgConcepto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                dlgConcepto dialog = new dlgConcepto(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCuenta;
    private javax.swing.JButton btnactualizar;
    private javax.swing.JButton btngrupo;
    private javax.swing.JButton btnguardar;
    private javax.swing.JButton btnnuevo;
    private javax.swing.JButton btnsalir;
    public static javax.swing.JComboBox cbogrupo;
    private javax.swing.JComboBox cbotipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar4;
    private javax.swing.JTextField txtconcepto;
    private javax.swing.JTextField txtcuenta;
    private javax.swing.JTextField txtprecio;
    // End of variables declaration//GEN-END:variables
}
